//
//  LLStatusOriginalView.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/27.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit
import SnapKit

class LLStatusOriginalView: UIView {
    
    //记录约束
     var originalViewBottomConstraint: Constraint?
    
    var status: LLStatusesModel? {
        
        didSet{
            
            //设置头像
            headImageView.sd_setImageWithURL(NSURL(string:   (status?.user?.profile_image_url) ?? ""),placeholderImage: UIImage(named: "avatar_default"))
            //昵称
            nameLable.text = status?.user?.name
            
            //认证图片
            expiredImageView.image = status?.user?.verifiedImage
            
            //vip图片
            vipImageView.image = status?.user?.mbrankImage
            
            //内容
            contentLabel.attributedText = status?.textAttr
            
            //时间
            timeLable.text = status?.createdAtStr
            
            //和来源
            sourceLabel.attributedText = status?.sourceAttri
            
            //判断是否有配图
            originalViewBottomConstraint?.uninstall()
            
            if status?.pic_urls?.count > 0 {
                
                pictureView.hidden = false
                
                pictureView.pic_urls = status?.pic_urls
                
                self.snp_makeConstraints { (make) -> Void in
                    
                    originalViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(OriginalMargin).constraint
                }
            }else{
                
                pictureView.hidden = true
                
                self.snp_makeConstraints{ (make) -> Void in
                    
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(OriginalMargin).constraint
                }
            }
        }
    }

       //微博来源
    func getSourceStr(source: String?) -> NSMutableAttributedString{
        guard let s = source where s.containsString("\">") else{
            
            let r = "来自 星星的你"
            return getSoureAttri(r)
        }
        
        let start = s.rangeOfString("\">")!
        
        let end = s.rangeOfString("</a>")!
        
        let resutl = s.substringWithRange(start.endIndex..<end.startIndex)
        
        let r = "来自 \(resutl)"
        
        return getSoureAttri(r)
    }
    
    //富文本
    func getSoureAttri(result:String) -> NSMutableAttributedString{
        
        let attri = NSMutableAttributedString(string: result)
        
        let range = (result as NSString).rangeOfString("来自")
        
        attri.addAttributes([NSForegroundColorAttributeName:ThemeColor], range: range)
        
        return attri
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setUI(){
        
        addSubview(headImageView)
        addSubview(nameLable)
        addSubview(expiredImageView)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(OriginalMargin)
            
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        
        nameLable.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(headImageView.snp_right).offset(OriginalMargin)
            
            make.top.equalTo(headImageView)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLable.snp_right).offset(OriginalMargin)
            
            make.centerY.equalTo(nameLable)
        }
        
        
        timeLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLable)
            
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(timeLable)
            
            make.left.equalTo(timeLable.snp_right).offset(OriginalMargin)
        }
        
        expiredImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(headImageView.snp_bottom)
            
            make.centerX.equalTo(headImageView.snp_right)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(OriginalMargin)
            
            make.top.equalTo(headImageView.snp_bottom).offset(OriginalMargin)
            
        }
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(OriginalMargin)
            
            make.leading.equalTo(contentLabel)
            

        }
        
        self.snp_makeConstraints { (make) -> Void in
            
           originalViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(OriginalMargin).constraint
        }
        
        
        
        nameLable.text = "昵称"
        timeLable.text = "时间"
        sourceLabel.text = "来源"
        contentLabel.text = "懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件懒加载子控件"
    }
    
    //懒加载子控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default_big")
    
    //昵称
    private lazy var nameLable: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    
    //等级common_icon_membership_expired
    private lazy var expiredImageView: UIImageView = UIImageView(imageName: "common_icon_membership_expired")
    
    //发微博时间
    private lazy var timeLable: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    
    //vip
    private lazy var vipImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    
    //微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 2*OriginalMargin)
    
    //微博配图
    private lazy var pictureView: LLPictureView = LLPictureView()
    
    
    
    
}

    
